<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
   const tree = [
  {
    id: 1,
    title: '研发部',
    pid: null,
    children: [
      {
        id: 5,
        title: '前端研发部',
        pid: 1,
        children: [
          {
            id: 10,
            title: '人工智能',
            pid: 2
          }
        ]
      },
      { id: 6, title: '后端研发部', pid: 1 },
      { id: 7, title: '算法研发部', pid: 1 }
    ]
  },
  {
    id: 2,
    title: '研发部',
    pid: null,
    children: [
      { id: 5, title: '前端研发部', pid: 2 },
      { id: 6, title: '后端研发部', pid: 2 },
      { id: 7, title: '算法研发部', pid: 2 }
    ]
  },
  {
    id: 3,
    title: '市场部',
    pid: null
  },
  {
    id: 4,
    title: '销售部',
    pid: null
  }
]
console.log(tree);
function treeToArray(tree) {
  const obj = []
  tree.forEach((item) => {
    if (item.children) {
      obj.push( item, ...item.children )
      // ES6新增的 删除对象的属性 Reflect.deleteProperty(对象，属性名)
      Reflect.deleteProperty(item,'children')
    } else {
      obj.push(item)
    }
  })
  return obj
}
console.log(treeToArray(treeToArray(tree)))

  </script>
</body>
</html>